<!-- 购物卡结佣管理-购物卡结佣-->
<template>
  <div class="shoppingCardSettlement">
    <el-form :inline="true" :model="searchParams" ref="searchParams">
      <el-form-item label="结算单ID：">
        <el-input clearable v-model="searchParams.batchCode" placeholder="请输入结算单ID"></el-input>
      </el-form-item>
      <el-form-item label="经纪人：">
        <el-input clearable v-model="searchParams.brokerName" placeholder="请输入经纪人姓名/电话"></el-input>
      </el-form-item>
      <el-form-item label="所属楼盘：">
        <el-input clearable v-model="searchParams.devName" placeholder="请输入楼盘名称"></el-input>
      </el-form-item>
      <el-form-item label="来源类别：">
        <el-select clearable v-model="searchParams.sourceCategory" placeholder="请选择">
          <el-option
            v-for="item in category"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结佣状态：">
        <el-select clearable v-model="searchParams.settleStatus" placeholder="请选择">
          <el-option
            v-for="item in status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间：">
        <el-date-picker
          v-model="time"
          clearable
          unlink-panels
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy - MM - dd HH:mm"
          value-format="yyyy-MM-dd HH:mm"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="推送状态：">
        <el-select clearable v-model="searchParams.pushFlag" placeholder="请选择">
          <el-option
            v-for="item in tstatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch" id="monitorEnter">查询</el-button>
        <el-button @click="onClear">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
      <el-table-column align="center" width="150" label="结算单id">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.sourceCategory == 2" @click="handleClick(scope.row)" type="text" size="small">{{scope.row.batchCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="orderNum" label="订单数"></el-table-column>
      <el-table-column align="center" prop="devName" label="楼盘"></el-table-column>
      <el-table-column align="center" prop="brokerName" label="经纪人"></el-table-column>
      <el-table-column align="center" prop="companyName" label="所属公司"></el-table-column>
      <el-table-column align="center" prop="applyAmount" label="购物卡总额">
        <template slot-scope="scope">
          <span>{{`￥${scope.row.applyAmount}`}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :formatter="commissionStatus"  label="结佣状态"></el-table-column>
      <el-table-column align="center" prop="settleDesc" label="备注"></el-table-column>
      <el-table-column align="center" :formatter="sourceStatus"  label="来源类别"></el-table-column>
      <el-table-column align="center" :formatter="checkStatus"  label="审核状态"></el-table-column>
      <el-table-column align="center" prop="operateName" label="操作人"></el-table-column>
      <el-table-column align="center" :formatter="formatterTime" label="操作时间"></el-table-column>
      <el-table-column align="center" :formatter="EAStatus" label="推送EAS状态" ></el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="commission(scope.row.settleStatus,scope.row.batchCode,scope.row.sourceCategory)" type="text" size="small">{{commissionText(scope.row.settleStatus)}}</el-button>
          <el-button :disabled="Prohibit(scope.row.settleStatus,scope.row.checkStatus)" @click="Approval(scope.row.batchCode,scope.row.sourceCategory)" type="text" size="small">发起审批</el-button>
          <el-button @click="goRecord(scope.row.batchCode)" type="text" size="small">结佣记录</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagebar">
      <el-pagination
        background
        layout="prev, pager, next, sizes, jumper"
        :current-page.sync="tableConfig.pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="tableConfig.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import api from "@/api/apiPath";
import utils from '@/common/utils'
export default {
  name: "shoppingCardSettlement",
  title:'购物卡结佣',
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      time: [],
      category: [
        {
          value: 0,
          label: "经纪人佣金"
        },
        {
          value: 1,
          label: "经纪人奖励"
        },
        {
          value: 2,
          label: "项目推荐奖"
        }
      ],
      tstatus: [
        {
          value: 0,
          label: "未推送"
        },
        {
          value: 1,
          label: "推送成功"
        },
        {
          value: 2,
          label: "推送失败"
        }
      ],
      status: [
        {
          value: 0,
          label: "结佣完成待确认"
        },
        {
          value: 1,
          label: "结佣失败"
        },
        {
          value: 2,
          label: "结佣成功"
        }
      ],
      searchParams: {
        batchCode: "",
        brokerName: "",
        devName: "",
        settleStatus: "",
        pushFlag: "",
        endTime: "",
        startTime: "",
        sourceCategory: "",
        pageSize: 10,
        pageNumber: 1
      },
      tableData: [],
      tableConfig: {
        pageSize: 10,
        pageNumber: 1
      },
      total: 0
    };
  },
  created() {
    this.list();
  },
  activated(){
    this.onSearch()
  },
  methods: {
    // 跳转经纪人详情
    handleClick(row) {
      this.$router.push({path:'./brokerAwardDetail',query:{id:row.batchId}})
    },
    // 格式化时间
    formatterTime(row) {
      return utils.getTimeString(row.operateTime)
    },
    // 发起审批
    Approval(batchCode,sourceCategory) {
      this.$confirm('确定要进行此操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post(api.initiateApproval,{batchCode:batchCode,terminalSource:sourceCategory}).then(res => {
            console.log(res)
            if(res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '操作成功'
              })
              this.list()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
      
    },
    Prohibit(settleStatus,checkStatus) {
      if(settleStatus == 2 && (checkStatus == 0 || checkStatus == 4)) {
        return false
      }else {
        return true
      }
    },
    // 来源类别
    sourceStatus(row) {
      if(row.sourceCategory === 0) {
        return '经纪人佣金'
      }else if(row.sourceCategory === 1) {
        return '经纪人奖励'
      }else if(row.sourceCategory === 2) {
        return '项目推荐奖'
      }
    },
    checkStatus(row) {
      if(row.checkStatus == 0) {
        return '未提交'
      }else if(row.checkStatus == 1) {
        return '待审核'
      }else if(row.checkStatus == 2) {
        return '审核中'
      }else if(row.checkStatus == 3) {
        return '审核完成'
      }else if(row.checkStatus == 4) {
        return '审核失败'
      }
    },
    // 判断EAS推送状态
    EAStatus(row) {
      if(row.pushFlag === 0) {
        return '未推送'
      }else if(row.pushFlag === 1) {
        return '推送成功'
      }else if(row.pushFlag === 2) {
        return '推送失败'
      }
    },
    // 判断结佣状态
    commissionStatus(row) {
      if(row.settleStatus === 0) {
        return '结佣完成待确认'
      }else if(row.settleStatus === 1) {
        return '结佣失败'
      }else if(row.settleStatus === 2) {
        return '结佣成功'
      }
    },
    // 判断状态
    commissionText(status) {
      if (status == 0) {
        return "结佣确认";
      } else if (status === 1) {
        return "结佣";
      } else {
        return "";
      }
    },
    handleSizeChange(size) {
      this.searchParams.pageSize = size;
      this.list();
    },
    handleCurrentChange(page) {
      this.searchParams.pageNum = page;
      this.list();
    },
    list() {
      this.axios.post(api.getSettleCommList, this.searchParams).then(data => {
        let res = data.data;
        if (res.code === 200) {
          this.tableData = res.data.list;
          this.tableData.forEach(item => {
            item.applyAmount = utils.formatMoney(item.applyAmount)
          })
          this.total = res.data.total;
        }
      });
    },
    onSearch() {
      this.searchParams.pageNum = 1;
      let [startTime,endTime] = this.time || ['','']; 
      this.searchParams.startTime = new Date(startTime).getTime();
      this.searchParams.endTime = new Date(endTime).getTime();
      this.list();
    },
    onClear() {
      (this.searchParams = {
        settlementId: "",
        agent: "",
        floorName: "",
        type: "",
        status: "",
        pushState: ""
      }),
      (this.time = []);
      this.list()
    },
    // 结佣记录
    goRecord(batchCode) {
      this.$router.push({
        path: "./shoppingCardSettlementRecord",
        query: {batchCode:batchCode}
      });
    },
    // 结佣/结佣待确认 跳转
    commission(settleStatus,batchCode,sourceCategory) {
      if(settleStatus == 1) {
        this.$router.push({
          path: "./shoppingCardSettlementCommission",
          query: {batchCode:batchCode,sourceCategory:sourceCategory}
        })
      } else {
        // 结佣待确认
        this.$router.push({
          path: "./shoppingCardSettlementTobeConfirm",
          query: {batchCode:batchCode,sourceCategory:sourceCategory}
        })
      }
      
    }
  },
  components: {},
  computed: {}
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="stylus" scoped>
.el-button 
  margin-left 0 
</style>
